<?php

use Illuminate\Database\Capsule\Manager as DB;

#[等级管理]
class AgentlevelController extends AcommonController
{

    public function init()
    {
        $this->model      = new agentlevelModel();
        $this->table      = 'agent_level';
        $this->primaryKey = 'level';
        parent::init();
    }

    #[添加等级]
    public function addAction()
    {
        if (!$this->isPost()) {
            $table                = $this->getTableColumn($this->table);
            $table['award_ping1'] = ['rate' => '', 'money' => ''];
            $table['award_ping2'] = ['rate' => '', 'money' => ''];
            $table['award_ping3'] = ['rate' => '', 'money' => ''];
            $this->_view->assign('dataset', json_encode($table));
            $this->_view->assign('levels', $this->model->get()->toJson());
            $this->_view->display(strtolower($this->controller) . '/edit.html');
        } else {
            $this->postData['award_ping1'] = json_encode($this->postData['award_ping1'], JSON_UNESCAPED_UNICODE);
            $this->postData['award_ping2'] = json_encode($this->postData['award_ping2'], JSON_UNESCAPED_UNICODE);
            $this->postData['upgrade_rules'] = json_encode($this->postData['upgrade_rules'], JSON_UNESCAPED_UNICODE);
            do {
                $rows = $this->postData;
                $rows['created_at'] = date('Y-m-d H:i:s');
                unset($rows[$this->primaryKey], $rows['updated_at']);
                $this->cleanColumn($rows);
                if(is_object($this->model)){
                    $query = $this->model;
                }else{
                    $query = is_string($this->model) ? (new $this->model) : DB::table($this->table);
                }
                if ($insert_id = $query->insertGetId($rows)) {
                    gudonglevelModel::insert(['level'=>$insert_id]);
                    $result = [
                        'ret' => 0,
                        'msg' => '操作成功',
                        'data' => $insert_id,
                    ];
                } else {
                    $result = [
                        'ret' => 3,
                        'msg' => '数据插入失败',
                    ];
                }
            } while (FALSE);
            json($result);
        }
    }

    #[编辑等级]
    public function editAction()
    {
        if ($this->isPost()) {
            $input = $this->postData;
            $roles = [
                $this->primaryKey => ['role' => "required|exists:{$this->table}.{$this->primaryKey}", 'msg' => '主键'],
                'name'            => ['role' => 'required', 'msg' => '等级名称'],
            ];
            Validate::check($input, $roles);
        } else {
            $this->_view->assign('levels', $this->model->get()->toJson());
        }
        parent::editAction();
    }


    #[代理删除]
    public function deleteAction()
    {
        // 删除gudong_level同等级记录
        $level = $this->get('level');
        gudonglevelModel::where('level', $level)->delete();

        parent::deleteAction();
    }

}
